<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Chat/classes/PhpFox_ComponentIm.class.php, $Author:  $ */



/** Chat component
 * 
 * @package module.chat
 * @version $Id: PhpFox_ComponentIm.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *   PHPFox Team ()
 */
class PhpFox_Mod_Chat_ComponentIm extends BaseComponent
{
    /** Processing method
     */
	function process()
	{
	    global $_CONF;
	    $_CONF['debug'] = 0;
	    $oReq = &App::getRequest();
	    /* @var $oReq Request */
	    if(App::getSetting('check_im'))
            App::gotoUrl('public');	    
        $oSrvChat = &$this->_oModule->getService('Chat');
        $oSrvAccount = &App::getModuleService('Account', 'Security');
        $oAcc        = &App::getModuleService('Account', 'Account');
        $oUser = $oSrvAccount->getCurrentUser();
        $oTpl = &$this->_oModule->getTemplate();
        $bWaiting = false;
        $bChat = false;

        if($oReq->getInt('chatId'))
        {
            switch ($oReq->getInt('approve'))
            {
                case 1:
                    $oSrvChat->setChatStatus($oReq->getInt('chatId'), 1);
                    App::gotoUrl('public.account.chat', array('chatId' => $oReq->getInt('chatId')));
                    break;
                case 2:
                    $oSrvChat->setChatStatus($oReq->getInt('chatId'), 2);
                    App::gotoUrl();
                    break;
            }
            $aChatUser = $oSrvChat->getUsers($oReq->getInt('chatId'));
            
            $oTpl->assignSrc(array(
                                    'iChatId'      => $oReq->getInt('chatId'),
                                    'sUser1'       => $aChatUser['user1'],
							        'sUser2'       => $aChatUser['user2'],
							        'sImageUrl'    => App::getParam('user.pic.url'),
                                    ));
            $srcHtml = $oTpl->fetch('ChatProc.html');
            return $srcHtml;
        }
        
        if($oReq->getInt('wait'))
        {
            $bWaiting = $oSrvChat->checkStatus($oReq->getInt('wait'));
            $this->addErrors($oSrvChat->getErrors());
            $aErrors = $this->getErrors();
            
            if((!$bWaiting && empty($aErrors)))
                App::gotoUrl('public.account.chat', array('chatId' => $oReq->getInt('wait')));
        }
        else 
        {
            // create instant messenger (check user status and clean expired messenger)
            if($oReq->get('call') && !$oReq->getInt('wait') && strtolower($oReq->get('call')) != strtolower($oUser->aData['user']))
            {
                $oUserCall = $oAcc->getByUserName($oReq->get('call'));
                $bIsOnline = $oAcc->isOnline($oUserCall->aData['user']);
                $bIsChat = $oSrvChat->checkCurrentChat($oUser->aData['user'], $oReq->get('call'));
                    
                if($bIsOnline)
                {
                    if(!$bIsChat)
                        $aErrors = $this->getErrors($this->addErrors(App::format('Mod_Chat.double.call',array($oUserCall->aData['user']))));
                    else 
                    {    
                        $iChatId = $oSrvChat->makeMessenger($oSrvAccount->getCurrentUserLogin(), $oUserCall->aData['user']);
                        App::gotoUrl('', array('call'=>$oUserCall->aData['user'], 'wait'=>$iChatId));
                    }
                }
                else 
                    $aErrors = $this->getErrors($this->addErrors(App::format('Mod_Chat.user.offline')));
            }
            if($oSrvAccount->getCurrentUserLogin() == $oUser->aData['user'])
            {
                $aOnline = $oSrvChat->getCaller($oSrvAccount->getCurrentUserLogin());
            }
            
           	$bOnline = empty($aOnline) ? $this->addErrors(App::format('Mod_Chat.empty.list')) : true; 
           	   
        }
        
        $oTpl->assign(array(
							 'bWaiting'     => $bWaiting,
							 'aErrors'		=> !empty($aErrors) ? $aErrors : $this->getErrors(),
							 'aOnline'      => $aOnline,
                             'bOnline'      => $bOnline,
                             'sImageUrl'    => App::getParam('user.pic.url'),
					 ));
					 
		$srcHtml = $oTpl->fetch('Chat.html');
		return $srcHtml;
	}
}
?>